//
//  CGLHandelNetWorkWithAF.m
//  HomeFurnishingDecoration
//
//  Created by 李劲成 on 16/4/28.
//  Copyright © 2016年 ljc. All rights reserved.
//

#import "NetHandle.h"
#import "AFNetworking.h"
#import "LocalCache.h"


@implementation NetHandle

/**
 @brief 处理 get数据
 
 @param urlStr      网址
 @param dic         网址 body
 @param responseObj  getUrl 为真时, 从网络获取的数据, 为假时 从本地读取的数据
 @param err         错误原因
 */
+ (void)GETWithURL:(NSString *)urlStr par:(NSDictionary *)dic header:(NSDictionary *)header success:(void (^)(id responseObj, BOOL getUrl))responseObj filed:(void (^)(NSError *))err {
    
    AFHTTPSessionManager *man = [AFHTTPSessionManager manager];
    man.requestSerializer.timeoutInterval = 40;
    LocalCache *cache = [LocalCache sharedLocalCache];
    
    // 有的返回的数据格式，AFN不支持解析，所以我们要设置一下。让AFN支持。
    [man.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
    
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    NSString *path = [NSString stringWithFormat:@"%ld",(unsigned long)[urlStr hash]];

    if (header) {
        for (NSString *keys in [header allKeys]) {
            [man.requestSerializer setValue:[header objectForKey:keys] forHTTPHeaderField:keys];
        }
    }
 
    
    
    [man GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
       
        [cache storeObject:responseObject toDiskforKey:path];
        
        responseObj(responseObject, YES);
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        err(error);

        
    }];

}



/**
 @brief 处理 post数据
 
 @param urlStr      网址
 @param dic         网址 body
 @param responseObj  getUrl 为真时, 从网络获取的数据, 为假时 从本地读取的数据
 @param err         错误原因
 */
+ (void)POSTWithURL:(NSString *)urlStr
                par:(NSDictionary *)dic
            success:(void(^)(id responseObject, BOOL getUrl))response
              filed:(void(^)(NSError *error))err {
   
    AFHTTPSessionManager *man = [AFHTTPSessionManager manager];
    LocalCache *cache = [LocalCache sharedLocalCache];
    [man.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
    
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSString *path = [NSString stringWithFormat:@"%ld",(unsigned long)[urlStr hash]];
    [man POST:urlStr parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [cache storeObject:responseObject toDiskforKey:path];
        response(responseObject, YES);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        id result = [cache readCachesFromDiskWithKey:path];
        err(error);
        response(result, NO);
        
    }];
    
}


@end
